<?php

namespace app\api\controller\v1;
use app\api\model\v1\Xcx;
use app\common\controller\Api;
use think\Request;

/**
 * 公共接口
 */
class Base extends Api
{
    protected $noNeedLogin = [];
    protected $reqParams = [];//请求的参数数组
    protected $user;
    public function __construct(Request $request = null){
        parent::__construct($request);
        $action = request()->action();
        if(!request()->isPost()){
            $this->error("请求失败");
        }
        $this->reqParams = request()->post();
        //来源小程序
        $referer = explode("/",$_SERVER['HTTP_REFERER']);
        $xcx = Xcx::where(['appid'=>$referer[3]])->cache(true)
            ->field("id as xcxid,name as xcxname,appid as xcxappid,secret as xcxsecret ")->find();
        if(!$xcx){
            $this->error("请求有误");
        }
        $this->reqParams = array_merge($this->reqParams,$xcx->toArray());
        if($this->noNeedLogin!="*" && !in_array(strtolower($action),$this->noNeedLogin)){
            $token = request()->post("token");
            if(!$token){
                $this->error("请登录后操作",'',-1);
            }
            $user = \app\api\model\v1\User::where(['token'=>$token])->find();
            if(!$user){
                $this->error("请登录后操作",'',-1);
            }
            if($user->expiretime<time()){
                $this->error("登录已过期，请重新登录",'',-2);
            }
            $this->user = $user;
        }

        //file_put_contents("req.txt",$action."-".date("Y-m-d H:i:s")."--".json_encode($this->reqParams)."\r\n",FILE_APPEND);

    }


}
